package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.entity.EduChapter;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.entity.chapter.ChapterVo;
import com.atguigu.eduservice.entity.chapter.VideoVo;
import com.atguigu.eduservice.mapper.EduChapterMapper;
import com.atguigu.eduservice.service.EduChapterService;
import com.atguigu.eduservice.service.EduVideoService;
import com.atguigu.servicebase.handler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author fxy
 * @since 2022-05-05
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Autowired
    private EduVideoService eduVideoService;

    //课程大纲列表,根据课程id进行查询
    @Override
    public List<ChapterVo> getChapterVideoByCourseId(String courseId) {

        //根据课程id查询里面所有章节
        QueryWrapper<EduChapter> chapterQueryWrapper=new QueryWrapper<>();
        chapterQueryWrapper.eq("course_id",courseId);
        List<EduChapter> chapterList = baseMapper.selectList(chapterQueryWrapper);

        //根据课程id查询里面所有的小节
        QueryWrapper<EduVideo> videoQueryWrapper=new QueryWrapper<>();
        videoQueryWrapper.eq("course_id",courseId);
        List<EduVideo> videoList = eduVideoService.list(videoQueryWrapper);

        //创建list集合，用于最终封装数据
        List<ChapterVo> list=new ArrayList<>();

        //遍历查询章节list集合进行封装
        //遍历查询出来的章节list
        for (int i = 0; i < chapterList.size(); i++) {
            EduChapter eduChapter = chapterList.get(i);
            ChapterVo chapterVo=new ChapterVo();
            BeanUtils.copyProperties(eduChapter,chapterVo);
            //把chapterVo放到最终List集合
            list.add(chapterVo);

            //创建集合，用于封装章节里面的小节
            List<VideoVo> videoVoList=new ArrayList<>();
            //遍历查询小节list集合，进行封装
            for (int j = 0; j < videoList.size(); j++) {
                EduVideo eduVideo = videoList.get(j);
                //判断：小节的chapterId是否和章节id一样
                if (eduVideo.getChapterId().equals(eduChapter.getId())){
                    //进行封装
                    VideoVo videoVo=new VideoVo();
                    BeanUtils.copyProperties(eduVideo,videoVo);

                    //放到小节封装的集合中去
                    videoVoList.add(videoVo);
                }
            }

            //把封装的之后的小节list集合，放到章节对象里面去
            chapterVo.setChildren(videoVoList);
        }

        return list;
    }

    @Override
    public boolean deleteChapter(String chapterId) {
        //根据chapterId => 章节id  查询小节表  如果查询有数据则不能删除
        QueryWrapper<EduVideo> wrapper=new QueryWrapper<>();
        wrapper.eq("chapter_id",chapterId);
        //count根据条件查询多少记录
        int count = eduVideoService.count(wrapper);
        if (count>0){//能查出小节，不进行删除
            throw new GuliException(20001,"请先删除小节，才能删除章节哦！！！");
        }else {//没有数据,可以删除
            int result = baseMapper.deleteById(chapterId);
            //成功则 1>0  失败则0>0
            return result>0;
        }

    }
    //根据课程id删除章节
    @Override
    public void removeChapterByCourseId(String courseId) {
        QueryWrapper<EduChapter>  wrapper=new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        baseMapper.delete(wrapper);
    }

}
